//#pragma once
#ifndef TEST_H
#define TEST_H
#include <QTcpServer>
#include <QObject>
#include <QDebug>
#include <QTcpSocket>
#include <QMap>
#include <queue>
#include <string>
#include <vector>
#include <iostream>
#include "dbProcess.h"
#include "Tcp_tools.h"


class TcpSocket : public QTcpSocket
{
    Q_OBJECT
public:
    static QMap<QString,TcpSocket*> tcpTree;
    TcpSocket(QObject *parent = nullptr)
        : QTcpSocket (parent)
    {
        connect(this, &QTcpSocket::readyRead, [this](){
            QString strCode=this->readAll();
            processObject sReadData_Object=Tcp_tools::string_to_processObject(strCode.toStdString());
            string userid=sReadData_Object.getUser();
            if(TcpSocket::tcpTree.count(QString::fromStdString(userid))==0){
                TcpSocket::tcpTree[QString::fromStdString(userid)]=this;
            }
            dbProcess::processQueue.push(sReadData_Object);
        });

        connect(this, &QTcpSocket::disconnected, [this](){
            emit emit_disConnected(this->socketDescriptor());
        });

    }

signals:
    void emit_disConnected(qintptr);
};

class TcpSocketServer:public QTcpServer
{
    Q_OBJECT
public:
    QList<TcpSocket*> socList;
    TcpSocketServer();
    virtual void incomingConnection(qintptr handle);
    void show_map();
public slots:
    void response_handle(processObject);
};

class ServerResponseThread : public QThread
{
    Q_OBJECT
public:
    virtual void run() override;
signals:
    void db_response_signal(processObject);
};

#endif
